//
//  Utils
//  StockWin
//
//  Created by showshaw on 15/6/15.
//  Copyright (c) 2015年 ZhongQian. All rights reserved.
//

#import "Utils.h"
#import "LoginViewController.h"
#import "BaseNavigationController.h"

//API接口地址
NSString * const kHostUrl = @"http://115.28.213.201:8088";
//NSString * const kHostUrl = @"";

NSString * const kUserLoginStatus = @"kUserLoginStatus";

#define kVersion ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"])

@implementation Utils

#pragma mark - 通用工具方法
/**
 *  获取Host地址
 */
+(NSString *)apiHostURL
{
    return kHostUrl;
}

/*
    获取指定长度随机字符串
 */
+ (NSString *)randomStr:(int)n
{
    char data[n];
    for (int x=0;x<n;data[x++] = (char)('A' + (arc4random_uniform(26))));
    return [[NSString alloc] initWithBytes:data length:n encoding:NSUTF8StringEncoding];
}
/*
    SHA1 加密
 */
+ (NSString *)sha1: (NSString *)input
{
    const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:input.length];
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1(data.bytes, data.length, digest);
    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return output;
}
/*
    时间戳
*/
+ (NSString *)timestamp
{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSString *timeString = [NSString stringWithFormat:@"%ld", (long)[dat timeIntervalSince1970]];
    return timeString;
}


+(void)checkLoginStatus:(UIViewController*)currentViewController
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    BOOL isLogin = [userDefault boolForKey:kUserLoginStatus];
    if(!isLogin)
    {
        [Utils jumpToLoginPage:currentViewController];
    }
}

+(void)jumpToLoginPage:(UIViewController*)currentViewController
{
    
    LoginViewController* loginVC = [[[NSBundle mainBundle] loadNibNamed:@"LoginViewController" owner:self options:nil] firstObject];
    
    BaseNavigationController * baseNavi = [[BaseNavigationController alloc] initWithRootViewController:loginVC];
    
    [currentViewController presentViewController:baseNavi animated:YES completion:NULL];
}


/**
 *  分割URI
 */
+(NSArray*)arrayBySplitURI:(NSString*)URI
{
    return [URI componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/-"]];
}

////正则字符串验证
+(BOOL)isRegularStr:(NSString *)str regex:(NSString *)regex;
{
    NSString * regexStr = regex;
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexStr];
    if (([regextestmobile evaluateWithObject:str] == YES)){
        return YES;
    }else
    {
        return NO;
    }
}

#pragma mark - 常用方法
/*
 通过Key获取API地址
 */
+ (NSString *)BaseUrlOfApi: (NSString *)key
{
    NSDictionary *dic = [[NSDictionary alloc] init];
    //todo: add all api
    dic =  [NSDictionary dictionaryWithObjectsAndKeys:
            //首页广告
            [NSString stringWithFormat:@"%@/ad/list",kHostUrl],@"ad-list",
            //直播列表
            [NSString stringWithFormat:@"%@/live/list",kHostUrl],@"live-list",
            //分类项目
            [NSString stringWithFormat:@"%@/category/list",kHostUrl],@"category-list",
            nil];
    return  ([[dic allKeys] containsObject:key]) ? dic[key] : nil;
}

@end
